package net.boar.os;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zgl
 * @Date: 2021/6/23 10:38
 * @Description:
 */
public class Floppy {
    enum MAGNETIC_HEAD{
        MAGNETIC_HEAD_0,
        MAGNETIC_HEAD_1
    }
    public int SECTOR_SIZE = 512;
    public int CYLINDER_COUNT = 80;
    public int SECTIRS_COUNT = 18;
    private MAGNETIC_HEAD magneticHead = MAGNETIC_HEAD.MAGNETIC_HEAD_0;
    private int current_cylinder = 0;
    private int current_sector = 0;
    private Map<Integer, List<List<byte[]>>> floppy = new HashMap<>();
    public Floppy(){
        floppy.put(MAGNETIC_HEAD.MAGNETIC_HEAD_0.ordinal(), initFloppyDisk());
        floppy.put(MAGNETIC_HEAD.MAGNETIC_HEAD_1.ordinal(), initFloppyDisk());
    }
    private List<List<byte[]>> initFloppyDisk(){
        List<List<byte[]>> floppyDisk = new ArrayList<>();
        for (int i = 0; i < CYLINDER_COUNT; i++) {
            floppyDisk.add(initCylinder());
        }
        return floppyDisk;
    }
    private List<byte[]> initCylinder(){
        List<byte[]> cylinder = new ArrayList<>();
        for (int i = 0; i < SECTIRS_COUNT; i++) {
            byte[] sector = new byte[SECTOR_SIZE];
            cylinder.add(sector);
        }
        return cylinder;
    }
    public void setMagneticHead(MAGNETIC_HEAD head){
        magneticHead = head;
    }
    public void setCylinder(int cylinder){
        if(cylinder < 0){
            this.current_cylinder = 0;
        }else if(cylinder >= 80){
            this.current_cylinder = 79;
        }else {
            this.current_cylinder = cylinder;
        }
    }
    public void setSector(int sector){
        if(sector < 0){
            this.current_sector = 0;
        }else if(sector > 18){
            this.current_sector = 18 -1;
        }else {
            this.current_sector = sector - 1;
        }
    }
    public byte[] readFloppy(MAGNETIC_HEAD head,int cylinderNum,int sectorNum){
        setMagneticHead(head);
        setCylinder(cylinderNum);
        setSector(sectorNum);
        List<List<byte[]>> disk = floppy.get(this.magneticHead.ordinal());
        List<byte[]> cylinder = disk.get(this.current_cylinder);
        return cylinder.get(this.current_sector);
    }
    public void writeFloppy(MAGNETIC_HEAD head,int cylinderNum,int sectorNum,byte[] buff){
        setMagneticHead(head);
        setCylinder(cylinderNum);
        setSector(sectorNum);
        List<List<byte[]>> disk = floppy.get(this.magneticHead.ordinal());
        List<byte[]> cylinder = disk.get(this.current_cylinder);
        byte[] b = new byte[512];
        System.arraycopy(buff,0,b,0,buff.length);
        cylinder.set(this.current_sector,b);
    }
    public void makeFloppy(String fileName){
        try {
            DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
            for (int head = 0; head <= MAGNETIC_HEAD.MAGNETIC_HEAD_1.ordinal(); head++) {
                for (int cylinder = 0; cylinder < CYLINDER_COUNT; cylinder++) {
                    for (int sector = 1; sector <= SECTIRS_COUNT; sector++) {
                        //0,1
                        byte[] buf = readFloppy(MAGNETIC_HEAD.values()[head],cylinder,sector);
                        out.write(buf);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
